-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[GISel][SDAG] Avoid push_back in loops for some shuffle mask handling. #119434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Each call to push_back contains a check to see if the vector needs to grow. Using resize or the giving the size to the constructor can reduce the number of checks for growing.
|
@llvm/pr-subscribers-llvm-globalisel Author: Craig Topper (topperc) ChangesEach call to push_back contains a check to see if the vector needs to grow. Using resize or the giving the size to the constructor can reduce the number of checks for growing. Full diff: https://github.com/llvm/llvm-project/pull/119434.diff 2 Files Affected:
diff --git a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
index 2544f2479ddc68..6676cee8f618a8 100644
--- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -6173,8 +6173,7 @@ LegalizerHelper::equalizeVectorShuffleLengths(MachineInstr &MI) {
// Extend mask to match new destination vector size with
// undef values.
SmallVector<int, 16> NewMask(Mask);
- for (unsigned I = MaskNumElts; I < SrcNumElts; ++I)
- NewMask.push_back(-1);
+ NewMask.resize(SrcNumElts, -1);
moreElementsVectorDst(MI, SrcTy, 0);
MIRBuilder.setInstrAndDebugLoc(MI);
@@ -6254,16 +6253,14 @@ LegalizerHelper::moreElementsVectorShuffle(MachineInstr &MI,
moreElementsVectorSrc(MI, MoreTy, 2);
// Adjust mask based on new input vector length.
- SmallVector<int, 16> NewMask;
+ SmallVector<int, 16> NewMask(WidenNumElts, -1);
for (unsigned I = 0; I != NumElts; ++I) {
int Idx = Mask[I];
if (Idx < static_cast<int>(NumElts))
- NewMask.push_back(Idx);
+ NewMask[I] = Idx;
else
- NewMask.push_back(Idx - NumElts + WidenNumElts);
+ NewMask[I] = Idx - NumElts + WidenNumElts;
}
- for (unsigned I = NumElts; I != WidenNumElts; ++I)
- NewMask.push_back(-1);
moreElementsVectorDst(MI, MoreTy, 0);
MIRBuilder.setInstrAndDebugLoc(MI);
MIRBuilder.buildShuffleVector(MI.getOperand(0).getReg(),
diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
index 465128099f4447..205ad899e4c278 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
@@ -6421,16 +6421,14 @@ SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N) {
SDValue InOp2 = GetWidenedVector(N->getOperand(1));
// Adjust mask based on new input vector length.
- SmallVector<int, 16> NewMask;
+ SmallVector<int, 16> NewMask(WidenNumElts, -1);
for (unsigned i = 0; i != NumElts; ++i) {
int Idx = N->getMaskElt(i);
if (Idx < (int)NumElts)
- NewMask.push_back(Idx);
+ NewMask[i] = Idx;
else
- NewMask.push_back(Idx - NumElts + WidenNumElts);
+ NewMask[i] = Idx - NumElts + WidenNumElts;
}
- for (unsigned i = NumElts; i != WidenNumElts; ++i)
- NewMask.push_back(-1);
return DAG.getVectorShuffle(WidenVT, dl, InOp1, InOp2, NewMask);
}
@@ -6478,12 +6476,9 @@ SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_REVERSE(SDNode *N) {
// Use VECTOR_SHUFFLE to combine new vector from 'ReverseVal' for
// fixed-vectors.
- SmallVector<int, 16> Mask;
- for (unsigned i = 0; i != VTNumElts; ++i) {
- Mask.push_back(IdxVal + i);
- }
- for (unsigned i = VTNumElts; i != WidenNumElts; ++i)
- Mask.push_back(-1);
+ SmallVector<int, 16> Mask(VTNumElts, -1);
+ for (unsigned i = 0; i != VTNumElts; ++i)
+ Mask[i] = IdxVal + i;
return DAG.getVectorShuffle(WidenVT, dl, ReverseVal, DAG.getUNDEF(WidenVT),
Mask);
|
|
@llvm/pr-subscribers-llvm-selectiondag Author: Craig Topper (topperc) ChangesEach call to push_back contains a check to see if the vector needs to grow. Using resize or the giving the size to the constructor can reduce the number of checks for growing. Full diff: https://github.com/llvm/llvm-project/pull/119434.diff 2 Files Affected:
diff --git a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
index 2544f2479ddc68..6676cee8f618a8 100644
--- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -6173,8 +6173,7 @@ LegalizerHelper::equalizeVectorShuffleLengths(MachineInstr &MI) {
// Extend mask to match new destination vector size with
// undef values.
SmallVector<int, 16> NewMask(Mask);
- for (unsigned I = MaskNumElts; I < SrcNumElts; ++I)
- NewMask.push_back(-1);
+ NewMask.resize(SrcNumElts, -1);
moreElementsVectorDst(MI, SrcTy, 0);
MIRBuilder.setInstrAndDebugLoc(MI);
@@ -6254,16 +6253,14 @@ LegalizerHelper::moreElementsVectorShuffle(MachineInstr &MI,
moreElementsVectorSrc(MI, MoreTy, 2);
// Adjust mask based on new input vector length.
- SmallVector<int, 16> NewMask;
+ SmallVector<int, 16> NewMask(WidenNumElts, -1);
for (unsigned I = 0; I != NumElts; ++I) {
int Idx = Mask[I];
if (Idx < static_cast<int>(NumElts))
- NewMask.push_back(Idx);
+ NewMask[I] = Idx;
else
- NewMask.push_back(Idx - NumElts + WidenNumElts);
+ NewMask[I] = Idx - NumElts + WidenNumElts;
}
- for (unsigned I = NumElts; I != WidenNumElts; ++I)
- NewMask.push_back(-1);
moreElementsVectorDst(MI, MoreTy, 0);
MIRBuilder.setInstrAndDebugLoc(MI);
MIRBuilder.buildShuffleVector(MI.getOperand(0).getReg(),
diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
index 465128099f4447..205ad899e4c278 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
@@ -6421,16 +6421,14 @@ SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N) {
SDValue InOp2 = GetWidenedVector(N->getOperand(1));
// Adjust mask based on new input vector length.
- SmallVector<int, 16> NewMask;
+ SmallVector<int, 16> NewMask(WidenNumElts, -1);
for (unsigned i = 0; i != NumElts; ++i) {
int Idx = N->getMaskElt(i);
if (Idx < (int)NumElts)
- NewMask.push_back(Idx);
+ NewMask[i] = Idx;
else
- NewMask.push_back(Idx - NumElts + WidenNumElts);
+ NewMask[i] = Idx - NumElts + WidenNumElts;
}
- for (unsigned i = NumElts; i != WidenNumElts; ++i)
- NewMask.push_back(-1);
return DAG.getVectorShuffle(WidenVT, dl, InOp1, InOp2, NewMask);
}
@@ -6478,12 +6476,9 @@ SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_REVERSE(SDNode *N) {
// Use VECTOR_SHUFFLE to combine new vector from 'ReverseVal' for
// fixed-vectors.
- SmallVector<int, 16> Mask;
- for (unsigned i = 0; i != VTNumElts; ++i) {
- Mask.push_back(IdxVal + i);
- }
- for (unsigned i = VTNumElts; i != WidenNumElts; ++i)
- Mask.push_back(-1);
+ SmallVector<int, 16> Mask(VTNumElts, -1);
+ for (unsigned i = 0; i != VTNumElts; ++i)
+ Mask[i] = IdxVal + i;
return DAG.getVectorShuffle(WidenVT, dl, ReverseVal, DAG.getUNDEF(WidenVT),
Mask);
|
| SmallVector<int, 16> NewMask(Mask); | ||
| for (unsigned I = MaskNumElts; I < SrcNumElts; ++I) | ||
| NewMask.push_back(-1); | ||
| NewMask.resize(SrcNumElts, -1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the constructor variant as you did in moreElementsVectorShuffle()?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're already using the constructor to copy the first part of the Mask. We could do
SmallVector<int, 16> NewMask(SrcNumElts, -1);
std::copy(NewMask.begin(), Mask.begin(), Mask.end())
Then we'd have the correct size at the start.
| for (unsigned i = VTNumElts; i != WidenNumElts; ++i) | ||
| Mask.push_back(-1); | ||
| SmallVector<int, 16> Mask(VTNumElts, -1); | ||
| for (unsigned i = 0; i != VTNumElts; ++i) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use std::iota?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
I messed up the size here. I should have used WidenNumElts in the constructor. Need to see why nothing failed.
| } | ||
| for (unsigned i = VTNumElts; i != WidenNumElts; ++i) | ||
| Mask.push_back(-1); | ||
| SmallVector<int, 16> Mask(WidenNumElts, -1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code isn't tested because VECTOR_REVERSE is converted to a generic VECTOR_SHUFFLE for fixed vectors.
Each call to push_back contains a check to see if the vector needs to grow. Using resize or the giving the size to the constructor can reduce the number of checks for growing.